package com.ruoyi.back.service.v3.impl

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.*
import com.ruoyi.back.domain.v3.TemplateReport
import com.ruoyi.back.mapper.TemplateReportMapper
import com.ruoyi.back.service.v3.MyRecordService
import com.ruoyi.back.service.v3.TemplateReportService
import com.ruoyi.common.core.page.TableSupport
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class TemplateReportServiceImpl : ServiceImpl<TemplateReportMapper, TemplateReport>(), TemplateReportService {

    @Autowired
    private lateinit var myRecordService: MyRecordService

    override fun saveTemplateReport(recordReportDto: TemplateReportService.RecordReportDto): Data<Boolean> =
        Ok("举报成功", save(TemplateReport().apply {
            createBy = id()
            recordId = recordReportDto.recordId
            reason = recordReportDto.reportContent
            myRecordService.getById(recordReportDto.recordId)?.let {
                username = it.username
                userInfoId = it.userInfoId
                pics = it.pics
                descText = it.descText
            }
        }))

    override fun templateReportPage(templateReportSearchDto: TemplateReportService.TemplateReportSearchDto): PageData<TemplateReportService.TemplateReportListVo> =
        TableSupport.buildPageRequest().let { pageDomain ->
            page(Page(pageDomain.pageNum.toLong(), pageDomain.pageSize.toLong()),
                KtQueryWrapper(TemplateReport::class.java).apply {
                    eq(
                    StrUtil.isNotBlank(templateReportSearchDto.userInfo),
                        TemplateReport::username,
                        templateReportSearchDto.userInfo
                )
                    .or().eq(
                        StrUtil.isNotBlank(templateReportSearchDto.userInfo),
                            TemplateReport::userInfoId,
                            templateReportSearchDto.userInfo
                    )
                    like(
                        StrUtil.isNotBlank(templateReportSearchDto.descText),
                        TemplateReport::descText,
                        templateReportSearchDto.descText
                    )
                    gt(
                        StrUtil.isNotBlank(templateReportSearchDto.reportBeginDate),
                        TemplateReport::createTime,
                        templateReportSearchDto.reportBeginDate.date()
                    )
                    lt(
                        StrUtil.isNotBlank(templateReportSearchDto.reportEndDate),
                        TemplateReport::createTime,
                        templateReportSearchDto.reportEndDate.date()
                    )
                    orderByDesc(TemplateReport::createTime)
                }).let { page: Page<TemplateReport> ->
                Page<TemplateReportService.TemplateReportListVo>().apply {
                    BeanUtil.copyProperties(page, this)
                    records = page.records.takeIf { it.isNotEmpty() }?.run {
                        map {
                            TemplateReportService.TemplateReportListVo().apply {
                                BeanUtil.copyProperties(it, this)
                                createTime = it.createTime?.format()
                            }
                        }
                    }
                }.pageInfo()
            }
        }

    override fun checkTemplateReport(checkTemplateDto: TemplateReportService.CheckTemplateDto): Data<Boolean> =
        getById(checkTemplateDto.id)?.let { templateReport ->
            updateById(templateReport.apply { status = checkTemplateDto.status })
            when (checkTemplateDto.status) {
                1 -> {
                    myRecordService.upRecord(templateReport.recordId!!)
                }
                else -> {
                    Ok("忽略成功", true)
                }
            }

        } ?: Fail("暂无资源")


    override fun removeCheckTemplate(ids: String): Data<Boolean> =
        Ok("移除成功", removeByIds(ids.split(",")))
}